package net.jxiny.oschina.MY.Fragment;

import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import net.jxiny.oschina.LoginActivity;
import net.jxiny.oschina.MY.Myself;
import net.jxiny.oschina.NetView.netView;
import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;
import net.jxiny.oschina.synthetical.Adapter.AnswerAdapter;
import net.jxiny.oschina.synthetical.Result.AnswerResult;
import net.jxiny.oschina.util.ACache;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyFragment extends Fragment {
    private Unbinder binder;


@BindView(R.id.myself_name)
     TextView nametext;

@BindView(R.id.igButton)
     ImageView headimg;
    private netView netView;
    private String[] titles = {"社区活跃度", "社区影响力", "技术贡献度", "活动活跃性", "开源贡献度", "学习积极性"};
    private double[] percent = {1, 1, 1, 1, 1, 1};


    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private ImageButton login;



    public MyFragment() {
    }


    public static MyFragment newInstance(String param1) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        super.onCreate(savedInstanceState);




    }

    private View view;
    private ListView lv2;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);

        binder= ButterKnife.bind(this,view);

        if (view == null) {
            view = inflater.inflate(R.layout.fragment_me, container, false);
        }
        login = view.findViewById(R.id.igButton);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });


        //0.获取数据
        initData();
        //1.初始化控件
        lv2 = view.findViewById(R.id.listView2);
        //2.创建Adapter

        final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, datas);
        //3.给ListView设置Adatper
        lv2.setAdapter(adapter);
        //4.给Item设置监听事件
        lv2.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                String data = (String) parent.getItemAtPosition(position);
                Toast.makeText(parent.getContext(), data, Toast.LENGTH_LONG).show();

            }
        });
        lv2.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
                datas.remove(parent.getItemAtPosition(position));
                adapter.notifyDataSetChanged();
                return true;
            }
        });
        netView = view.findViewById(R.id.netView);
        initView();
        return view;
    }

    private void initView() {

        netView.setTitles(titles);
        netView.setPercent(percent);
    }


    private List<String> datas;


    private void initData() {
        datas = new ArrayList<>();
        datas.add("我的消息");
        datas.add("我的勋章");
        datas.add("阅读记录");
        datas.add("我的博客");
        datas.add("我的灰名单");
        datas.add("我的问答");
        datas.add("我的投递");
        datas.add("我的活动");
        datas.add("关注标签");
        datas.add("邀请好友");

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_USER +
                        ACache.get(getActivity()).getAsString("token")).build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {

                        Myself myself = info.getRetDetail(Myself.class);
                        nametext.setText(myself.getName());
                        Glide.with(getActivity()).load(myself.getAvatar()).into(headimg);

                    }
                    @Override
                    public void onFailure(HttpInfo info) throws IOException {
                        Toast.makeText(getContext(),"发送失败"+info,Toast.LENGTH_LONG).show();

                    }
                });


    }
}
